package org.example.MyBatisPlus.Service.Impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Conrtoller.ChineseUtil;
import org.example.MyBatisPlus.Mapper.ConfigMapper;
import org.example.MyBatisPlus.Mapper.AdUserMapper;
import org.example.MyBatisPlus.Service.IUserService;
import org.example.pojos.AdminPojo;
import org.example.pojos.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author : zhangbo
 * @data : 2024/7/29
 * @description :动态生成id，redis自增，同步锁
 */
@Service
public class MyUserServiceImpl extends ServiceImpl<AdUserMapper, AdminPojo> implements IUserService {

    private final String key = "RANDOMUUID";
    private final String INIT_ID = "0";
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ConfigMapper configMapper;
    @Autowired
    private IUserService  userService;

    /**
     * @return
     */
    @Override
    public List<String> generateId() {
        /**
         * 生成id要求，当日001-？？？，明日001-？？，可以直观看出每日新增id个数
         */
        List<String>list =new ArrayList<>();
        StringBuffer timeStr = new StringBuffer();
        redisTemplate.opsForSet().add(key,INIT_ID);
        //设置过期时间一天
        redisTemplate.expire(key,Duration.ofSeconds(86400));
        //模拟10条id
        for (int i = 0; i < 10; i++) {
            String now = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            timeStr.append(now);
            //使用redis实现自增id后缀
            String randomId;
            //存入redis
            Long incr = getIncr(key);
            redisTemplate.opsForSet().add(key,String.valueOf(incr));
            timeStr.append(String.valueOf(incr));
        }
        list.add(timeStr.toString());
        return list;
    }

    /**根据输入数量，自动生成用户信息
     * @param num
     * @return
     */
    @Override
    public ResultResponse generateByNum(Integer num) {
        Date date = new Date(); // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        localDateTime = localDateTime.plusHours(20); // 增加20小时
        Date newDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); // 转换回Date
        List<AdminPojo>collect=new ArrayList<>(num);
        for (int i=0; i<=num ;i++) {
        //构建对象信息
          AdminPojo ad = new AdminPojo();
          ad.setUsername(ChineseUtil.randomChineseName());
          ad.setPassword(ChineseUtil.randomPassword());
          ad.setPhone(ChineseUtil.randomPhone());
          ad.setLoginTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
          ad.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
          ad.setExitTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate));
          ad.setStatus("1");
          ad.setUpdatePerson("张博");
          collect.add(ad);
    }
        userService.saveBatch(collect);
        return new ResultResponse(true,collect);
    }

    Long getIncr(String key) {
        Long result = redisTemplate.execute((RedisCallback<Long>) redisConnection -> {
            return redisConnection.incr(key.getBytes());
        });
        return result;
    }

    public static void main(String[] args) {
        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        // 格式化日期
        String formattedDate = today.format(formatter);

        // 打印结果
        System.out.println("格式化的日期: " + formattedDate);

    }
}
